﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

namespace Songhay.OpenXml.Transformations
{
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Transforms the Open XML WordML table elements.
    /// </summary>
    [Export(typeof(IWordProcessingDocumentTransformation))]
    public class TableTransformation : IWordProcessingDocumentTransformation
    {
        #region IWordProcessingDocumentTransformation Members

        /// <summary>
        /// Gets or sets the execution ordinal.
        /// </summary>
        /// <value>The execution ordinal.</value>
        public byte ExecutionOrdinal { get; set; }

        /// <summary>
        /// Determines whether this instance can transform the specified word doc.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can transform the specified word doc; otherwise, <c>false</c>.
        /// </returns>
        public bool CanTransform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node)
        {
            return this.HasElementName(node, SonghayW.tbl) ||
                this.HasElementName(node, SonghayW.tr) ||
                this.HasElementName(node, SonghayW.tc);
        }

        /// <summary>
        /// Transforms the specified Word document.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <param name="imageHandler">The image handler.</param>
        public object Transform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node,
            Func<ImageInfo, XElement> imageHandler)
        {
            var element = node as XElement;

            return TransformTable(wordDoc, settings, imageHandler, element);
        }

        #endregion

        private static object TransformTable(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings,
            Func<ImageInfo, XElement> imageHandler, XElement element)
        {
            if(element.Name == SonghayW.tbl)
            {
                // Transform w:tbl to h:tbl.
                return new XElement(SonghayXhtml.table,
                    new XAttribute(SonghayXhtml.@class, "WordWalkingStickTable"),
                    element.Elements()
                        .Select(e => OpenXmlUtility.TransformPackageNode(wordDoc, settings, e, imageHandler)));
            }

            if(element.Name == SonghayW.tr)
            {
                // Transform w:tr to h:tr.
                return new XElement(SonghayXhtml.tr,
                    element.Elements()
                        .Select(e => OpenXmlUtility.TransformPackageNode(wordDoc, settings, e, imageHandler)));
            }

            if(element.Name == SonghayW.tc)
            {
                // Transform w:tc to h:td.
                return new XElement(SonghayXhtml.td,
                    element.Elements()
                        .Select(e => OpenXmlUtility.TransformPackageNode(wordDoc, settings, e, imageHandler)));
            }

            return null;
        }
    }
}
